#include <stdio.h>

// 前置声明函数
void quicksort(int a[], int low, int high);
int partition(int a[], int low, int high);

int main()
{
    int a[10] = {5, 3, 8, 6, 2, 7, 1, 9, 4, 0};
    printf("Before sorting: \n");
    for (int i = 0; i < 10; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\n");

    quicksort(a, 0, 9);

    printf("After sorting: \n");
    for (int i = 0; i < 10; i++)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}

void quicksort(int a[], int low, int high)
{
    if (low < high)
    {
        int mid = partition(a, low, high); // 声明 mid 为局部变量
        quicksort(a, low, mid - 1);
        quicksort(a, mid + 1, high);
    }
}

int partition(int a[], int low, int high)
{
    int pivot = a[high];
    int i = low - 1;
    int temp; // 声明 temp 为局部变量

    for (int j = low; j < high; j++)
    {
        if (a[j] < pivot)
        {
            i++;
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }

    temp = a[i + 1];
    a[i + 1] = a[high];
    a[high] = temp;
    return i + 1;
}